# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Dimitris Kiziridis <ragouel at outlook dot com>
# Contributor: Sebastian Wilzbach < sebi at wilzbach dot me >
# Contributor: Daniel Micay <danielmicay@gmail.com>
# Contributor: Ivy Foster <joyfulgirl (at) archlinux (dot) us>
# Contributor: Stefan Husmann <stefan-husmann@t-online.de>

pkgname=bibutils
pkgver=7.2
pkgrel=2
pkgdesc="Bibliography format interconversion"
arch=('loong64' 'x86_64')
url="http://sourceforge.net/p/bibutils/home/Bibutils"
license=('GPL2')
depends=('glibc')
makedepends=('docbook-xsl' 'libxslt')
source=(
  "${pkgname}-${pkgver}.tar.gz::https://downloads.sourceforge.net/${pkgname}/${pkgname}_${pkgver}_src.tgz"
  'generate-man-pages.patch'
)
sha512sums=('6a8d1e616fee6a8aff38792cfb9d9b1797b5729d356c0e5651d713b2631ac81d2b287602026bbc4e8f52762f3b2a2d35b2da339f2387c98c2fbc99bb47c3b21e'
            '57c8502504e085997591f9ed8238efba82ed9647942a71b09005f4a6b94a098d084c5c02d7d32ec3078b50e88a5fb499115a53daae3b44e50d038dbe7457330b')
b2sums=('e58e740e8e68e72e7d1bd84cd45e50add17f18fabf42d737df0c4206ea085eb2368ff585136cb1f4c5c8ccc337041778f46a39381f557c0bee37fe7817d9c538'
        '16ccdaa056836210b9a88c3293798525d48e5c0edeb6d1c376420314fad80c8f80f621c8f91aee13de17056ac0ab850c2d0963f10d130bfc6bb791f88f2a0cd0')

prepare() {
  cd "${pkgname}_${pkgver}"

  # remove erroneous backslash from pkgconfig
  sed -i -e 's:\\::g' packageconfig_start

  # generate man pages for all commands
  patch -p1 -i ../generate-man-pages.patch
}

build() {
  cd "${pkgname}_${pkgver}"

  ./configure \
    --dynamic \
    --install-dir "$pkgdir/usr/bin" \
    --install-lib "$pkgdir/usr/lib"

  make DISTRO_CFLAGS="$CFLAGS" LDFLAGSIN="$LDFLAGS"

  # generate man pages
  local DBVER="$(ls -1 /usr/share/xml/docbook | grep nons | sed -e 's/^xsl-stylesheets-//' -e 's/-nons$//')"
  xsltproc \
    --output bibutils.1 \
    --nonet \
    "/usr/share/xml/docbook/xsl-stylesheets-$DBVER-nons/manpages/docbook.xsl" \
    bibutils.dbk
}

check() {
  cd "${pkgname}_${pkgver}"
  make DISTRO_CFLAGS="$CFLAGS" LDFLAGSIN="$LDFLAGS" test
}

package() {
  cd "${pkgname}_${pkgver}"

  # binaries/shared objects
  make install

  # headers
  install -vDm644 -t "$pkgdir/usr/include/$pkgname" lib/*.h

  # pkgconfig
  install -vDm644 -t "$pkgdir/usr/lib/pkgconfig" "lib/$pkgname.pc"

  # man pages
  install -vDm644 -t "$pkgdir/usr/share/man/man1" *.1

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" ChangeLog
}
